package com.cskaoyan.service;

import com.cskaoyan.bean.bo.AdminCategoryCreateBO;
import com.cskaoyan.bean.bo.AdminCategoryUpdateBO;
import com.cskaoyan.bean.bo.AdminCategoryUpdateChlidrenBO;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketCategory;
import com.cskaoyan.bean.po.MarketCategoryExample;
import com.cskaoyan.bean.vo.admin_category.AdminCategoryChildrenListVO;
import com.cskaoyan.bean.vo.admin_category.AdminCategoryCreateVO;
import com.cskaoyan.bean.vo.admin_category.AdminCategoryL1ListVO;
import com.cskaoyan.bean.vo.admin_category.AdminCategoryListVO;
import com.cskaoyan.mapper.MarketCategoryMapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: Ryan
 * @CreateTime: 2022-09-07  19:30
 */
@Service
public class AdminCategoryServiceImpl implements AdminCategoryService{
    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Override
    public CommonData<AdminCategoryListVO> selectAllAdminCategoryList() {
        List<MarketCategory> marketCategoriesList = marketCategoryMapper.selectByExample(new MarketCategoryExample());

        PageInfo<MarketCategory> pageInfo = new PageInfo<>(marketCategoriesList);
        int total = pageInfo.getSize();
        int pages = pageInfo.getPages();
        int limit = pageInfo.getPageSize();
        int page = pageInfo.getPageNum();

        LinkedList<AdminCategoryListVO> categoryListVOS = new LinkedList<>();
        //单个数据
        for (MarketCategory marketCategory : marketCategoriesList) {
            //外层数据
            if (marketCategory.getPid() == 0) {
                //通过父亲ID连接到子的pid 查询到子表
                List<AdminCategoryChildrenListVO> categoryChildrenVOList = marketCategoryMapper.selectByPidKey(marketCategory.getId());
                categoryListVOS.add(new AdminCategoryListVO(marketCategory.getPicUrl(), marketCategory.getKeywords(), marketCategory.getLevel(), marketCategory.getName(), marketCategory.getId(), marketCategory.getIconUrl(), marketCategory.getDesc(), categoryChildrenVOList));
            }
        }
        CommonData<AdminCategoryListVO> commonData = new CommonData<>(total,pages,limit,page,categoryListVOS);
        return commonData;
    }

    @Override
    public AdminCategoryCreateVO createAdminCategory(AdminCategoryCreateBO categoryCreateBO) {
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setDesc(categoryCreateBO.getDesc());
        marketCategory.setIconUrl(categoryCreateBO.getIconUrl());
        marketCategory.setKeywords(categoryCreateBO.getKeywords());
        marketCategory.setLevel(categoryCreateBO.getLevel());
        marketCategory.setName(categoryCreateBO.getName());
        marketCategory.setPicUrl(categoryCreateBO.getPicUrl());
        marketCategory.setPid(categoryCreateBO.getPid());

        marketCategoryMapper.insertSelective(marketCategory);
        AdminCategoryCreateVO categoryCreateVO = new AdminCategoryCreateVO(marketCategory.getPicUrl(),marketCategory.getKeywords(),new Date(System.currentTimeMillis()),marketCategory.getLevel(),marketCategory.getName(),marketCategory.getPid(),new Date(System.currentTimeMillis()),marketCategory.getId(),marketCategory.getIconUrl(),marketCategory.getDesc());
        return categoryCreateVO;
    }

    @Override
    public CommonData<AdminCategoryL1ListVO> getL1AdminCategory() {
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        PageInfo<MarketCategory> pageInfo = new PageInfo<>(marketCategories);
        int total = pageInfo.getSize();
        int pages = pageInfo.getPages();
        int limit = pageInfo.getPageSize();
        int page = pageInfo.getPageNum();

        List<AdminCategoryL1ListVO> l1ListVOS = new LinkedList<>();
        for (MarketCategory category : marketCategories) {
            if (category.getPid() == 0) {
                l1ListVOS.add(new AdminCategoryL1ListVO(category.getName(),category.getId()));
            }
        }
        CommonData<AdminCategoryL1ListVO> commonData = new CommonData<>(total,pages,limit,page,l1ListVOS);
        return commonData;
    }

    @Override
    public void updateAdminCategory(AdminCategoryUpdateBO categoryUpdateBO) {
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setId(categoryUpdateBO.getId());
        marketCategory.setName(categoryUpdateBO.getName());
        marketCategory.setKeywords(categoryUpdateBO.getKeywords());
        marketCategory.setDesc(categoryUpdateBO.getDesc());
        marketCategory.setIconUrl(categoryUpdateBO.getIconUrl());
        marketCategory.setPicUrl(categoryUpdateBO.getPicUrl());
        marketCategory.setLevel(categoryUpdateBO.getLevel());
        marketCategory.setPid(categoryUpdateBO.getPid());

        marketCategoryMapper.updateByPrimaryKeySelective(marketCategory);

        List<AdminCategoryUpdateChlidrenBO> childrenList = categoryUpdateBO.getChildren();

        for (AdminCategoryUpdateChlidrenBO chlidrenBO : childrenList) {
            MarketCategory marketCategory2 = new MarketCategory();
            marketCategory2.setId(chlidrenBO.getId());
            marketCategory2.setName(chlidrenBO.getName());
            marketCategory2.setKeywords(chlidrenBO.getKeywords());
            marketCategory2.setDesc(chlidrenBO.getDesc());
            marketCategory2.setIconUrl(chlidrenBO.getIconUrl());
            marketCategory2.setPicUrl(chlidrenBO.getPicUrl());
            marketCategory2.setLevel(chlidrenBO.getLevel());
            marketCategoryMapper.updateByPrimaryKeySelective(marketCategory2);
        }
    }

    @Override
    public void deleteAdminCategory(AdminCategoryUpdateBO categoryUpdateBO) {
        List<AdminCategoryUpdateChlidrenBO> children = categoryUpdateBO.getChildren();
        //若为一级类目 则删除一级类目及所属的二级类目
        if(children!=null) {
            for (AdminCategoryUpdateChlidrenBO child : children) {
                marketCategoryMapper.deleteByPrimaryKey(child.getId());
            }
        }
        marketCategoryMapper.deleteByPrimaryKey(categoryUpdateBO.getId());
    }


}
